<template>
  <c-dialog ref="dialog" :open-fun="openFun" title="发起异常" @open="openHandle">
    <c-list ref="form" :model="formData" :rules="formRules" label-width="120px">
      <c-list-item prop="exceptionHandleId" label="异常类型">
        <el-select v-model="formData.exceptionHandleId" placeholder="异常类型" clearable @change="changeHandle">
          <el-option v-for="(e,i) in exceptionList" :key="i" :label="e.exceptionType" :value="e.id" />
        </el-select>
      </c-list-item>
      <c-list-item v-if="typeCode==='EXCEPT007'" prop="afterAdjustTime" label="预计完成时间">
        <el-date-picker
          v-model="formData.afterAdjustTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择日期"
        />
      </c-list-item>

      <c-list-item prop="exceptionDesc" label="异常说明">
        <el-input v-model="formData.exceptionDesc" type="textarea" maxlength="1024" show-word-limit :autosize="{minRows: 3}" placeholder="异常说明" clearable />
      </c-list-item>
      <c-list-item>
        <div class="btn-container">
          <c-button @click="cancelHandle">取消</c-button>
          <c-button type="primary" @click="confirmHandle">确认</c-button>
        </div>
      </c-list-item>
    </c-list>
  </c-dialog>
</template>
<script>
import { postExceptionType } from '@/api/decorate'
import { postShopException } from '@/api/shopDrawing'
import { otherFiltersItem } from '@/utils'
export default {
  name: 'Abnormal',
  props: {
    selData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      exceptionList: [],
      typeCode: '',
      formData: {
        exceptionHandleId: '',
        afterAdjustTime: '',
        exceptionDesc: '',
        bpmInstanceNo: ''
      },
      formRules: {
        exceptionHandleId: [{ required: true, message: '请选择异常类型', trigger: 'change' }],
        afterAdjustTime: [{ required: true, message: '请选择预计完成时间', trigger: 'change' }],
        exceptionDesc: [{ required: true, message: '请填写异常说明', trigger: 'change' }]
      }
    }
  },

  methods: {
    openFun() {
      if (!this.singleJudge(this.selData, '发起异常', true)) {
        return false
      }
      const d = this.selData[0]
      if (d.state === 'ABNORMAL') {
        this.$message.warning('状态为异常暂停，不能发起异常！')
        return false
      }
      return true
    },
    openHandle() {
      const data = new FormData()
      data.append('pageFlag', false)
      data.append('filtersRaw', JSON.stringify([
        otherFiltersItem('belongModel', 'SHOP_DESIGN', '='),
        otherFiltersItem('typeCode', '[EXCEPT007,EXCEPT008]', 'in')
      ]))
      postExceptionType(data).then(res => {
        this.exceptionList = res.data.records
      })
    },
    confirmHandle() {
      return this.$refs.form.validate().then(() => {
        const d = this.selData[0]
        const t = this.$gsed()
        t.exceptionDesc = this.formData.exceptionDesc
        if (this.typeCode === 'EXCEPT007') {
          t.afterAdjustTime = this.formData.afterAdjustTime
          t.beforeAdjustTime = d.expectEndTime
        }
        t.remark = ''
        if (this.typeCode === 'EXCEPT008') {
          t.stopTime = this.$parseTime(new Date())
        }
        const data = {
          bizCode: d.picTaskCode,
          bizName: d.picTaskName,
          constCode: d.constCode,
          exceptionHandleId: this.formData.exceptionHandleId,
          exceptionDesc: this.formData.exceptionDesc,
          modelId: d.id,
          bpmInstanceNo: this.formData.bpmInstanceNo || '',
          shopExceptionDetailDTOS: [t]
        }
        return postShopException(data).then(res => {
          this.$message.success('操作成功！')
          this.$refs.dialog.visible = false
          this.$emit('on-success')
        })
      })
    },
    changeHandle(v) {
      for (const c of this.exceptionList) {
        if (c.id === v) {
          this.typeCode = c.typeCode
          if (c.isBpmReview === '1' || c.isBpmReview === 1) {
            this.$set(this.formData, 'bpmInstanceNo', v.processCode || '')
          } else {
            this.$set(this.formData, 'bpmInstanceNo', '')
          }
          break
        }
      }
    },
    cancelHandle() {
      this.$refs.dialog.visible = false
    }
  }
}
</script>
